import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color, filters
from scipy import ndimage

def show_img(img):
    plt.imshow(img)
    plt.axis('off')
    plt.show()


# 1. 读取图像并转换为灰度
image = io.imread("../img/test4.jpg")
gray_image = color.rgb2gray(image)
show_img(gray_image)

# 2. 高斯平滑减少噪声
# smooth_image = filters.gaussian(gray_image, sigma=2)

# 3. 计算图像梯度（电场方向）
grad_y, grad_x = np.gradient(gray_image)

# 电场方向垂直于等势面（梯度旋转90度）
# 旋转梯度向量得到垂直方向（电场方向）
e_field_x = -grad_y  # 旋转90度
e_field_y = grad_x   # 旋转90度
#
# # 4. 创建网格
# h, w = gray_image.shape
# Y, X = np.mgrid[0:h, 0:w]
#
# # 5. 绘制结果
# plt.figure(figsize=(10, 8))
#
# # 显示原始等势面
# plt.imshow(image, alpha=0.7)
#
# # 绘制电场线（流线）
# seed_points = np.zeros((h, w), dtype=bool)
# seed_points[::20, ::20] = True  # 每20像素取一个种子点
#
# plt.streamplot(X, Y, e_field_x, e_field_y,
#                density=3,
#                color='red',
#                linewidth=1,
#                arrowsize=1.5,
#                start_points=np.argwhere(seed_points)[:, ::-1])
#
# plt.axis('off')
# plt.tight_layout()
# # plt.savefig("electric_field_lines.png", dpi=150, bbox_inches='tight')
# plt.show()